import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import metamap.proxy.client.MetamapClient;
import umls.interfaces.MySql;
import umls.interfaces.PubMed;
import umls.interfaces.Pathways.Pathways;

public class Main {

	/**
	 * @param args
	 */
	public static MetamapClient mc = null;
	public static String result = "";

	public static void main(String[] args) {

		// MetaMapApi api = new MetaMapApiImpl();
		testPubmed();
		// testPathways();
		// TODO Auto-generated method stub

		
	}

	public static void testPathways() {
		Pathways pw = new Pathways();
		List<String> result = null;
		try {
			result = pw.getGenes("angiogenesis");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Trovati " + result.size() + " geni");
		for (String tmp : result) {
			System.out.println(tmp);
		}
	}

	public static void testUMLS(){
		try { 
			MySql database = new MySql("130.192.163.27", "abate", "7prova7",
		"umls"); 
		database.connect(); 
		List<String> result= new LinkedList<String>();
		List<String> AUIs = database.searchAUI("BCL2 gene");
		System.out.println("La ricerca ha generato "+AUIs.size()+" AUI");
		for (String s : AUIs){
		//	System.out.println(s+" --->"+database.getDefFromAUI(s));
			for (String sy : database.searchRelatedAUI(s)){
			System.out.println(sy+" --->"+database.getDefFromAUI(sy));
			}
			//result.add(database.getDefFromAUI(sy));
		}
		} 
		catch (SQLException e) 
		{ 
			e.printStackTrace(); 
		}


	}
	public static void testPubmed() {

		PubMed pm = null;
		try {
			pm = new PubMed("http://localhost:8080/Core/services/PubProxyPort");
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		List<String> genes = new LinkedList<String>();
		genes.add("VEGFA");
		long start_time = new Date().getTime();
		pm.getTerm(genes, "Angiogenesis");
		long end_time = new Date().getTime();
		int tempo = (int) ((end_time - start_time) / 1000);
		int secondi = tempo % 60;

		System.out.println("Calcolato in " + tempo / 60 + " min " + secondi
				+ " secondi");
		/*
		 * for (String a : pm.getTerm(genes, "Angiogenesis")) {
		 * System.out.println(a);
		 * 
		 * }
		 */
	}

	public static void testMetamap() {
		try {
			mc = new MetamapClient("http://localhost:7071/Metamap");
			FileReader fr = new FileReader("./metatest");
			BufferedReader in = new BufferedReader(fr);

			String str = new String();
			while ((str = in.readLine()) != null) {
				result += str;
			}
			for (int i = 0; i < 5; i++) {

				Thread t = new Thread(new Worker(result));
				t.start();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

class Worker implements Runnable {
	private String text;

	public Worker(String s) {
		text = s;
	}

	@Override
	public void run() {
		// MetamapClient.testWork(text);

	}

}